package com.gree.flowable.app.services.impl;

import com.alibaba.fastjson.JSONObject;
import com.gree.flowable.app.mapper.plus.FlwFormNodesetBusinessService;
import com.gree.flowable.app.pojo.form.FlwFormNodeset;
import com.gree.flowable.app.pojo.form.FlwFormNodesetBusiness;
import com.gree.flowable.app.pojo.form.FormModel;
import com.gree.flowable.app.pojo.vo.FlowElementVO;
import com.gree.flowable.app.pojo.vo.ReturnVO;
import com.gree.flowable.app.services.IFlowableBpmnModelService;
import com.gree.flowable.app.services.IFlwFormNodesetBusinessService;
import com.gree.flowable.app.services.IFormInfoService;
import com.gree.flowable.app.tools.constants.ReturnCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author ：180849
 * @date ：Created in 2021/9/7 10:01
 * @description：进行中的表单与流程实例
 * @modified By：
 * @version: 1.0
 */
@Service
public class IFlwFormNodesetBusinessServiceImpl implements IFlwFormNodesetBusinessService {
    @Autowired
    private IFlowableBpmnModelService iFlowableBpmnModelService;

    @Autowired
    private IFormInfoService iFormInfoService;

    @Autowired
    private FlwFormNodesetBusinessService flwFormNodesetBusinessService;
    /**
     * 添加进行中的表单与流程节点的关系
     *
     * @param processKey  流程定义ID
     * @param formId      表单ID
     * @param businessKey 审批单号
     * @return
     */
    @Override
    public boolean addFormNodeBusiness(String processKey, String formId, String businessKey) {
        ReturnVO result = null;
        //获取流程节点
        ReturnVO<List<FlowElementVO>> fLowElementByProceDefKey = iFlowableBpmnModelService.getFLowElementByProceDefKey(processKey);
        List<FlowElementVO> flowNodeList = fLowElementByProceDefKey.getData();
        JSONObject jsonObject = new JSONObject();
        List<FlwFormNodesetBusiness> formNodeBusinessList = new LinkedList<>();
        ReturnVO<List<FormModel>> formFieldByFormId = iFormInfoService.getFormFieldByFormId(formId);
        if (!"200".equals(formFieldByFormId.getCode())) {
            return false;
        } else {
            for (FlowElementVO flowElement : flowNodeList) {
                FlwFormNodesetBusiness flwFormNodesetBusiness = new FlwFormNodesetBusiness();
                //组装进行中的流程实例与表单关系数据
                String node = flowElement.getElementId();
                if ("S000".equals(node) || "S999".equals(node)) {
                    continue;
                }
                flwFormNodesetBusiness.setElementId(node);
                flwFormNodesetBusiness.setProcessKey(processKey);
                flwFormNodesetBusiness.setBusinessKey(businessKey);
                List<FormModel> formInfoList = formFieldByFormId.getData();
                for (FormModel formModel : formInfoList) {
                    if ("S001".equals(node)) {
                        jsonObject.put(formModel.getFields(), 2);
                    } else {
                        jsonObject.put(formModel.getFields(), 1);
                    }
                }
                flwFormNodesetBusiness.setFormDesc(jsonObject.toString());
                formNodeBusinessList.add(flwFormNodesetBusiness);
            }
        }
        boolean b = flwFormNodesetBusinessService.saveBatch(formNodeBusinessList);

        return b;
    }
}
